package com.css.fxfzxczh.modules.eqDisasterLossAssess.houseFinishLossAssess.repository.impl;

import com.css.fxfzxczh.modules.eqDisasterLossAssess.houseFinishLossAssess.entity.HouseFinishEconomicLoss;
import com.css.fxfzxczh.modules.eqDisasterLossAssess.houseFinishLossAssess.entity.MhFinishHouseArea;
import com.css.fxfzxczh.modules.eqDisasterLossAssess.houseFinishLossAssess.repository.HouseFinishEconomicLossRepository;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
import org.springframework.jdbc.core.namedparam.SqlParameterSource;
import org.springframework.jdbc.core.namedparam.SqlParameterSourceUtils;
import org.springframework.stereotype.Repository;

import javax.annotation.Resource;
import java.util.List;

/**
 * @Author:
 * @CreateTime: 2023/8/9 14:19
 * @Description:
 */
@Repository
public class HouseFinishEconomicLossRepositoryImpl implements HouseFinishEconomicLossRepository {

    @Resource
    JdbcTemplate jdbcTemplate;
    @Resource
    NamedParameterJdbcTemplate namedParameterJdbcTemplate;

    @Override
    public void batchSave(List<HouseFinishEconomicLoss> list) {
        SqlParameterSource[] beanSources = SqlParameterSourceUtils.createBatch(list.toArray());
        String sql="insert into xczh_house_finish_economic_loss(xhfel_id,xstm_id,xstm_name,structure_type,destroy_loss," +
                "serious_damage_loss,moderate_damage_loss,minor_damage_loss,basically_intact_loss,total_loss," +
                "create_user,create_time,xsem_id,ratio_type,type) " +
                "values(:xhfelId,:xstmId,:xstmName,:structureType,:destroyLoss,:seriousDamageLoss,:moderateDamageLoss," +
                ":minorDamageLoss,:basicallyIntactLoss,:totalLoss,:createUser,:createTime,:xsemId,:ratioType,:type)";
        /*String sql="insert into xczh_middle_high_finish_house_area(xhfmcr_id,xsem_id,xstm_id,xstm_name,total_area,create_user,create_time,concrete_house_area,masonry_house_area1,masonry_house_area2,ratio_type) " +
                "values(:xhfmcrId,:xsemId,:xstmId,:xstmName,:totalArea,:createUser,:createTime,:concreteHouseArea,:masonryHouseArea1,:masonryHouseArea2,:ratioType)";*/
        namedParameterJdbcTemplate.batchUpdate(sql, beanSources);
    }

    @Override
    public void deleteByXsemId(String xsemId,String type) {
        String sql="delete from xczh_house_finish_economic_loss where xsem_id = ? and type = ?";
        jdbcTemplate.update(sql, xsemId,type);
    }

    @Override
    public List<HouseFinishEconomicLoss> getSubZoneTotalLossByXsemId(String xsemId) {
        String sql="select xstm_name,xstm_id,sum(total_loss) totalLoss from xczh_house_finish_economic_loss where xsem_id= ? " +
                "and type = ? " +
                "group by xstm_id,xstm_name,create_time " +
                "order by create_time";
        List<HouseFinishEconomicLoss> query = jdbcTemplate.query(sql, new BeanPropertyRowMapper<>(HouseFinishEconomicLoss.class), xsemId,"0");
        return query;
    }

    @Override
    public List<HouseFinishEconomicLoss> getLossInfo(String xsemId, String type) {
        String sql="select * from xczh_house_finish_economic_loss where xsem_id = ? and type = ? order by create_time";
        List<HouseFinishEconomicLoss> query = jdbcTemplate.query(sql, new BeanPropertyRowMapper<>(HouseFinishEconomicLoss.class), xsemId,type);
        return query;
    }

}
